/*
 * This file is part of Annuaire.
 *
 * Annuaire is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Annuaire is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Annuaire.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.miage.annuaire.pdf;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.stream.StreamResult;
import org.miage.annuaire.data.XmlDataAccessImpl;

import org.junit.*;

/**
 *
 * @author comeulie
 */
public class xml2pdfTestold {
    static File baseDir = new File(".\\web\\WEB-INF\\configurationXML\\");
    Node node_xml = null;
    Node node_data = null;

    @Before
    public void setUp() {
        // Recherche des données a partir du fichier XML
        Document doc_xml = null;
        DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
           
        try {
            DocumentBuilder builder = fact.newDocumentBuilder();
            doc_xml = builder.parse("." + File.separator + "web"
            		+ File.separator + "WEB-INF" + File.separator
            		+ "configurationXML" + File.separator + "personnes.xml");//
        } catch(Exception e1 ) {
            try {
                DocumentBuilder builder = fact.newDocumentBuilder();
                doc_xml = builder.parse("." + File.separator + "web"
                		+ File.separator + "WEB-INF" + File.separator
                		+ "configurationXML" + File.separator
                		+ "personnes.xml");//
            } catch(Exception e2 ) {
                System.out.println("Erreur pendant le parsing du fichier de text personnes.xml: " + e1 + "\n" + e2);
            }                        
        }

        if (doc_xml == null) {
        	throw new NullPointerException();
        }
        
        // Recherche de sdocnnées a partir de la class XmlDataAccess
        XmlDataAccessImpl data = new XmlDataAccessImpl();
        Document doc_data = null;
        try {
            doc_data = data.getMembresByName("TOTO");
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(xml2pdf.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (doc_data == null) {
        	throw new NullPointerException();
        }
        
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = null;
        try {
            trans = transfac.newTransformer();
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(xml2pdf.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        if (trans == null) {
        	throw new NullPointerException();
        }
        
        trans.setOutputProperty(OutputKeys.METHOD, "xml");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(2));

        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(doc_data.getDocumentElement());
        try {
            trans.transform(source, result);
        } catch (TransformerException ex) {
            Logger.getLogger(xml2pdf.class.getName()).log(Level.SEVERE, null, ex);
        }

        this.node_xml = doc_xml.getDocumentElement();
        this.node_data = doc_data.getDocumentElement();
    }
    
    @Test
    public void testExtract2stream() {
        // 1) On test la fonction a partir d'un ficheir XML pre-rempli sur le HDDDocument doc_xml = null;
         //Node nodeXML, InputStream xsl
    }
     
    @Test
    public void testExtract2file() {
            xml2pdf xf = new xml2pdf();

            // Test depuis un fichier XML pre-rempli :
            xf.extract2file( this.node_xml, "annuaire_xml.pdf" );

            // Test depuis la class XmlDataAccessImpl
            xf.extract2file( this.node_data, "annuaire_data.pdf" );
    }
}
